package com.lanou.service.impl;

import com.lanou.bean.CollectMap;
import com.lanou.bean.User;
import com.lanou.mapper.UserCollectionMapper;
import com.lanou.service.UserCollectionService;
import com.lanou.utils.AjaxResult;
import com.lanou.utils.StrUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by dllo on 18/3/19.
 */
@Service
public class UserCollectionServiceImpl implements UserCollectionService {
    @Resource
    private UserCollectionMapper userCollectionMapper;

    /**
     *
     * @param albumName 创建的图集名字
     * @return 创建是否成功
     * 开发人:赵帅
     */
    @Override
    public AjaxResult createAlbum(String albumName) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode("200");
        ajaxResult.setMsg("创建收藏图片专辑");
        albumName = StrUtils.charSetStr(albumName);
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        System.out.println(user);
        Integer user_id = user.getU_id();
        userCollectionMapper.createAlbum(user_id, albumName);
        CollectMap collectMap= userCollectionMapper.getId(user_id);
        ajaxResult.setData(collectMap.getId());
        return ajaxResult;
    }

    /**
     *
     * @param map_id 收藏图集的id
     * @param src 收藏的图片地址
     * @return 收藏图片结果
     * 开发人:赵帅
     */
    @Override
    public AjaxResult insertMap(Integer map_id,String src) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode("200");
        ajaxResult.setMsg("点击收藏图片");
        ajaxResult.setData("收藏成功");
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Integer user_id = user.getU_id();
        CollectMap collectMap= userCollectionMapper.getId(user_id);
        userCollectionMapper.insertMap(collectMap.getId(), src);
        return ajaxResult;
    }

    /**
     *
     * @return 所有图片集合
     * 开发人:赵帅
     */
    @Override
    public AjaxResult showMap() {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode("200");
        ajaxResult.setMsg("查看收藏集的图片");
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Integer user_id = user.getU_id();
        ajaxResult.setData(userCollectionMapper.showMap(user_id));
        return ajaxResult;
    }

    /**
     *
     * @param albumName 修改后的名字
     * @return 修改结果
     * 开发人:赵帅
     */
    @Override
    public AjaxResult changeMapName( String albumName,Integer id) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode("200");
        ajaxResult.setMsg("修改专辑名字");
        ajaxResult.setData("修改成功");
        userCollectionMapper.changeCollectMapName(id,albumName);
        return ajaxResult;
    }

    /**
     *
     * @param id 专辑的id
     * @return 删除的结果
     * 开发人:赵帅
     */
    @Override
    public AjaxResult deleteMap(Integer id) {
        AjaxResult ajaxResult  =new AjaxResult();
        ajaxResult.setCode("200");
        ajaxResult.setMsg("删除专辑");
        ajaxResult.setData("删除成功");

        userCollectionMapper.deleteMap(id);
        return ajaxResult;
    }
}
